package cn.tedu.knows.portal.controller;


import cn.tedu.knows.portal.exception.ServiceException;
import cn.tedu.knows.portal.model.Answer;
import cn.tedu.knows.portal.model.User;
import cn.tedu.knows.portal.service.IAnswerService;
import cn.tedu.knows.portal.vo.AnswerVo;
import com.sun.org.apache.bcel.internal.generic.IASTORE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2021-07-23
 */
@RestController
@RequestMapping("/v1/answers")
@Slf4j
public class AnswerController {

    @Resource
    private IAnswerService answerService;

    @PostMapping("")
//  @PreAuthorize("hasAuthority('/question/answer')")
    @PreAuthorize("hasRole('ROLE_TEACHER')")
    public Answer postAnswer(
            @Validated AnswerVo answerVo,
            BindingResult result,
            @AuthenticationPrincipal UserDetails user){
        log.debug("接收表单信息:{}",answerVo);
        if(result.hasErrors()){
            String msg=result.getFieldError().getDefaultMessage();
            throw new ServiceException(msg);
        }
        Answer answer= answerService.saveAnswer(
                    answerVo , user.getUsername());
        return answer;
    }
    // /v1/answers/question/149
    @GetMapping("/question/{id}")
    public List<Answer> questionAnswers(
            @PathVariable Integer id){
        List<Answer> answers=answerService.getQuestionAnswers(id);
        return answers;

    }

    //学生采纳回答的方法
    @GetMapping("/{answerId}/solved")
    public String solved(
            @PathVariable Integer answerId,
            @AuthenticationPrincipal UserDetails user){
        boolean isSolved=answerService.accept(
                            answerId,user.getUsername());
        if(isSolved){
            return "ok";
        }else{
            return "您没有权限或已经采纳过";
        }
    }
}
